<?php
/**
 * This file contains all necessary code to view an SimpleLTI
 * activity instance
 *
 * @author Jordi Piguillem
 *
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package basiclti
 */

    require_once("../../config.php");
    require_once("lib.php");
	require_once("locallib.php");

	global $DB;

    $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
    $a  = optional_param('a', 0, PARAM_INT);  // basiclti ID

    if ($id) {
        if (! $cm = $DB->get_record("course_modules", array("id" => $id))) {
            error("Course Module ID was incorrect");
        }

        if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
            error("Course is misconfigured");
        }

        if (! $basiclti = $DB->get_record("basiclti", array("id" => $cm->instance))) {
            error("Course module is incorrect");
        }

    } else {
        if (! $basiclti = $DB->get_record("basiclti", array("id" => $a))) {
            error("Course module is incorrect");
        }
        if (! $course = $DB->get_record("course", array("id" => $basiclti->course))) {
            error("Course is misconfigured");
        }
        if (! $cm = get_coursemodule_from_instance("basiclti", $basiclti->id, $course->id)) {
            error("Course Module ID was incorrect");
        }
    }

    require_login($course->id);

    add_to_log($course->id, "basiclti", "view", "view.php?id=$cm->id", "$basiclti->id");

/// Print the page header
    $strbasicltis = get_string("modulenameplural", "basiclti");
    $strbasiclti  = get_string("modulename", "basiclti");

    $navlinks = array();
    $navlinks[] = array('name' => $strbasicltis, 'link' => "index.php?id=$course->id", 'type' => 'activity');
    $navlinks[] = array('name' => format_string($basiclti->name), 'link' => '', 'type' => 'activityinstance');

    $navigation = build_navigation($navlinks);

    print_header_simple(format_string($basiclti->name), "", $navigation, "", "", true,
                  update_module_button($cm->id, $course->id, $strbasiclti), navmenu($course, $cm));

/// Print the main part of the page

	print_box($basiclti->intro, 'generalbox description', 'intro');

	print_box_start('generalbox activity');
	basiclti_view($basiclti->id);
	print_box_end();


/// Finish the page
    print_footer($course);

?>
